package com.finger.eduservice.controller;


import com.finger.commonutils.R;
import com.finger.eduservice.client.VodClient;
import com.finger.eduservice.entity.EduVideo;
import com.finger.eduservice.exception.GuliException;
import com.finger.eduservice.service.EduVideoService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-06
 */
@Log4j2
@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;
    // 远程服务调用
    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节
     * @param eduVideo
     * @return
     */
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){

        eduVideoService.save(eduVideo);

        return R.ok();
    }

    /**
     * 根据小节Id删除小节
     * @param videoId
     * @return
     */
    @DeleteMapping("/deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId){

        EduVideo eduVideo = eduVideoService.getById(videoId);

        String videoSourceId = eduVideo.getVideoSourceId();

        if(!StringUtils.isEmpty(videoSourceId)){
            // 远程服务调用删除云端视频
            R result = vodClient.deleteVod(videoSourceId);
            if(!result.getSuccess()){
                throw new GuliException(20001, (String) result.getData().get("msg"));
            }
        }

        eduVideoService.removeById(videoId);
        return R.ok();
    }

    /**
     * 根据ID更新小节
     * @param eduVideo
     * @return
     */
    @PutMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        return R.ok().data("success",eduVideoService.updateById(eduVideo));
    }

    /**
     * 根据ID查找小节
     * @param videoId
     * @return
     */
    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable String videoId){
        return R.ok().data("video",eduVideoService.getById(videoId));
    }

}

